package com.lastoffer2.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.lastoffer2.R;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;
import com.lastct.dao.Offer;
import com.lastoffer2.util.JSONConnector;

import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.WorkSource;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class JobListAdapter extends BaseAdapter {
	SimpleDateFormat formatterLong = new SimpleDateFormat("EEE, d MMM HH:mm");
	public static List<Offer> listData = new ArrayList<Offer>();

	private LayoutInflater layoutInflater;

	private static Context context;

	public JobListAdapter(Context context, List<Offer> listData) {
		this.context = context;
		this.listData = listData;
		// new ArrayList<Offer>(JSONConnector.getListOffers());

		layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return listData.size();
	}

	@Override
	public Object getItem(int position) {
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item_menu, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.title);

			holder.headlineView = (TextView) convertView
					.findViewById(R.id.title);
			holder.description = (TextView) convertView
					.findViewById(R.id.descriprion);
			holder.startsIn = (TextView) convertView
					.findViewById(R.id.TextViewStart);
			holder.usefullFrom = (TextView) convertView
					.findViewById(R.id.TextViewusefullFrom);
			holder.DateView = (TextView) convertView.findViewById(R.id.date);
			holder.image = (ImageView) convertView
					.findViewById(R.id.imageView1);
			holder.distanza = (TextView) convertView
					.findViewById(R.id.TextViewDistanza);
			holder.numeroPosti = (TextView) convertView
					.findViewById(R.id.TextViewNumeroPosti);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.headlineView.setText(listData.get(position).getName());
		holder.description
				.setText(listData.get(position).getShortDescription());
		holder.DateView.setText(formatterLong.format(listData.get(position)
				.getDataInizioPrenotazione()));
		if (!listData.get(position).getImage().isEmpty())
			UrlImageViewHelper.setUrlDrawable(holder.image,
					listData.get(position).getImage());
		holder.distanza.setText(listData.get(position).getDistance());
		holder.startsIn.setText(getCountDown(listData.get(position)
				.getDataInizioPrenotazione()));
		holder.usefullFrom.setText(getCountDown(listData.get(position)
				.getDataInizioOfferta()));
		holder.numeroPosti.setText("" + (listData.get(position).getNumMax()));
		convertView.setTag(holder);
		return convertView;
	}

	public static String getCountDown(Date toDate) {
		String result = context.getResources().getString(
				R.string.count_down_offerta);
		long diffHours = ((toDate.getTime() - System.currentTimeMillis()) / (60 * 60 * 1000)) % 24;
		long diffMinutes = ((toDate.getTime() - System.currentTimeMillis()) / (60 * 1000)) % 60;
		String sDiffHours = diffHours < 10 ? ("0" + diffHours)
				: ("" + diffHours);
		String sDiffMinutes = diffMinutes < 10 ? ("0" + diffMinutes)
				: ("" + diffMinutes);
		if (diffHours >= 0 && diffMinutes >= 0)
			result = sDiffHours + ":" + sDiffMinutes;
		return result;

	}

	static class ViewHolder {
		public TextView title;
		TextView headlineView;
		TextView description;
		TextView startsIn;
		TextView usefullFrom;
		TextView DateView;
		ImageView image;
		TextView distanza;
		TextView numeroPosti;
	}

	public void dataHasChanged(List<Offer> data) {
		this.listData = data;
		this.notifyDataSetChanged();
	}

}
